import DRC.*;
import DRC.DRModbus.DRModbus_RTU_Slave;
import com.sun.net.httpserver.*;

import DRC.DRModbus.DRModbus_RTU_Master;
import DRC.DRModbus.DRModbus_TCP_Master;
import DRC.DRModbus.DRModbus_TCP_Slave;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.concurrent.Executors;

import javax.swing.SwingUtilities;

public class DRFJW_HttpServer {

    public static ArrayList<channel> channels=new ArrayList<channel>();
    public static ArrayList<channel4collect> channels4collect=new ArrayList<channel4collect>();

    public static void main(String[] args) throws IOException {
        int WEBPort=8080;
        DRCDB drcdb=new DRCDB();
        //jdk自带轻量级http server
        InetSocketAddress addr = new InetSocketAddress(WEBPort);
        HttpServer server = HttpServer.create(addr,0);
 
        // 启动http server
        System.out.printf("Server is listening on port %d\n",WEBPort);
        server.createContext("/",new DRFJW_Handler(drcdb));
        server.setExecutor(Executors.newCachedThreadPool());
        server.start();

        // 读取DRC配置，
        // 注意：由于服务通道中可能涉及采集通道数据，因此需要优先读取采集通道
        // 2024年6月13日 罗继东
        // 读取采集通道配置
        channels4collect=channel4collect.read_channels4collect(drcdb);
        // for(channel4collect c4c:channels4collects){
        //     for(dataItem4collect d4c:c4c.d4cs){
        //         if(d4c.dataItem_id==233){
        //             d4c.report();
        //             break;
        //         }
        //     }
        // }
        // 输出采集通道报告
        // for(channel4collect ch4c:channels4collects){
        //     ch4c.report();
        // }
        // 读取服务通道配置
        channels=channel.read_channels(drcdb,channels4collect);

        // 为协议配置通道对象列表
        dataItem.channels=channels;
        dataItem.channels4collect=channels4collect;
        dataItem4collect.channels=channels;
        dataItem4collect.channels4collect=channels4collect;

        DRModbus_TCP_Slave.channels=channels;
        DRModbus_TCP_Slave.channels4collect=channels4collect;
        DRModbus_TCP_Master.channels=channels;
        DRModbus_TCP_Master.channels4collect=channels4collect;
        DRModbus_RTU_Master.channels=channels;
        DRModbus_RTU_Master.channels4collect=channels4collect;
        DRModbus_RTU_Slave.channels=channels;
        DRModbus_RTU_Slave.channels4collect=channels4collect;
        dataItem2channel.channels4collect = channels4collect;
        dataItem2channel.channels = channels;
        saveChannel4collect.channels4collect = channels4collect;

//        dataSimulator.channels=channels;
        // 输出服务通道报告
        // for(channel ch:channels){
        //     ch.report();
        // }

        // 自动启动可启动通道
        channel.auto_run_channel(drcdb);
        System.out.println("服务通道启动！");
        channel4collect.data_collect_cycle=100;
        channel4collect.auto_run_channel4collect(drcdb);
        System.out.println("采集通道启动！");
        // DRFJW_frame swingApp = new DRFJW_frame("http://localhost:8080/");
        // SwingUtilities.invokeLater(swingApp::createAndShowWindow);

        saveChannel4collect.channels4collect = channels4collect;
        saveChannel4collect collect = new saveChannel4collect(drcdb);
        collect.start();
        System.out.println("采集数据入库启动");

//        // 启动模拟数据采集线程
//        dataSimulator.drcdb=drcdb;
//
//        dataSimulator.channels=channels;
//        dataSimulator.auto_start();
        // dataSimulator ds1=new dataSimulator(3072,10,-100,100,dataSimulator.DATA_TYPE_FLOAT,dataSimulator.METHOD_RANDOM,1000,null);
        // ds1.run_mark=true;
        // ds1.start();
        // dataSimulator ds2=new dataSimulator(0,10,0,1,dataSimulator.DATA_TYPE_FLOAT,dataSimulator.METHOD_INCREMENT,1000,null);
        // ds2.run_mark=true;
        // ds2.start();
    }
}
